﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class FastOutStockDetail : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();

       private string FBillNo;   
       private string WorkBillNo;     
       private string PkgsBarCode;
       private string ID;
       private string FInterid;
       private decimal ScanQty;
       private string len;
       private string FInterID;

        public FastOutStockDetail()
        {
            InitializeComponent();
        }

        public FastOutStockDetail(string _ID, string _FBillNo, string _FInterID)
        {
            InitializeComponent();
            this.ID = _ID;
            this.FBillNo = _FBillNo;
            this.tb_Stock.Text = _FBillNo;
            this.tb_Work.Focus();
            //this.tb_Work.Text = "SCRWD20190202001";
            ScanQty = bjs.GetFastOutStockScanQty(FBillNo);
            this.tb_ScanQty.Text = ScanQty.ToString();
            FInterID = _FInterID;

        }

        private void FastOutStockDetail_Load(object sender, EventArgs e)
        {
            DataGridStyle();
            BindData();           
        }

        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "FastOutStockDetail";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "rownum";
            cl1.Width = 30;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "包装码";
            cl2.MappingName = "PkgsBarCode";
            cl2.Width = 80;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "SKU";
            cl3.MappingName ="Fsku";
            cl3.Width = 130;
            dts.GridColumnStyles.Add(cl3);

              DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "物料名称";
            cl4.MappingName = "Fname";
            cl4.Width = 80;
            dts.GridColumnStyles.Add(cl4);

              DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
              cl5.HeaderText = "属性";
              cl5.MappingName = "AttrValue";
              cl5.Width = 80;
              dts.GridColumnStyles.Add(cl5);
  
            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "根长";
            cl6.MappingName = "StickLen";
            cl6.Width = 40;
            dts.GridColumnStyles.Add(cl6);

            DataGridTextBoxColumn cl7 = new DataGridTextBoxColumn();
            cl7.HeaderText = "米数/包";
            cl7.MappingName = "FQty";
            cl7.Width =60;
            dts.GridColumnStyles.Add(cl7);

            DataGridTextBoxColumn cl8 = new DataGridTextBoxColumn();
            cl8.HeaderText = "销售订单号";
            cl8.MappingName = "SaleBillNo";
            cl8.Width = 120;
            dts.GridColumnStyles.Add(cl8);

            DataGridTextBoxColumn cl9 = new DataGridTextBoxColumn();
            cl9.HeaderText = "生产任务单";
            cl9.MappingName = "WorkBillNo";
            cl9.Width = 120;
            dts.GridColumnStyles.Add(cl9);

            DataGridTextBoxColumn cl10 = new DataGridTextBoxColumn();
            cl10.HeaderText = "状态";
            cl10.MappingName = "PkgsStatus";
            cl10.Width = 30;
            dts.GridColumnStyles.Add(cl10);

            dataGrid1.TableStyles.Add(dts);
        }

        private void BindData()
        {
            DataSet ds = bjs.GetFastOutStockDetail(FBillNo);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            dataGrid1.DataSource = dt;
            if (dt.Rows.Count <= 0)
            {
                return;
            }
            else
            {
              dataGrid1.DataSource = dt;
            }
        }

        private void tb_PkgsBarCode_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PkgsBarCode = this.tb_PkgsBarCode.Text;
                if (PkgsBarCode == null || PkgsBarCode == "")
                {
                    MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    string  state  = bjs.GetBDCLen(PkgsBarCode,out len);
                    if (state == "0")
                    {
                        if (len != null && len != "")
                        {
                            len = len.Substring(0, len.Length - 1);
                        }
                        this.button1.Focus();
                    }
                    else if (state == "1")
                    {
                        MessageBox.Show("请确认包装码是否已存在且未汇报！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        this.tb_PkgsBarCode.Text = "";
                        return;
                    }
                    else if (state == "2")
                    {
                        MessageBox.Show("快速出库明细表中已经存在该包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        this.tb_PkgsBarCode.Text = "";
                        return;
                    }
                }
                   
            }
        }
       
        private void tb_Stock_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                FBillNo = this.tb_Stock.Text.Trim();
                if (FBillNo == null || FBillNo == "")
                {
                    MessageBox.Show("请扫描销售订单！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                this.tb_Work.Focus();
            }
        }

        private void tb_Work_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                WorkBillNo = this.tb_Work.Text.Trim();
                if (WorkBillNo == null || WorkBillNo == "")
                {
                    MessageBox.Show("请扫描生产单号", "提示信息！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                this.tb_PkgsBarCode.Focus();
            }
        }

        

        //提交
        private void button1_Click(object sender, EventArgs e)
        {
            bool result = false;
            FBillNo = this.tb_Stock.Text.ToString();
            WorkBillNo = this.tb_Work.Text.ToString();
            PkgsBarCode = this.tb_PkgsBarCode.Text.ToString();
            if (WorkBillNo == null || WorkBillNo == "")
            {
                MessageBox.Show("生产任务单号不能为空", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }
            result = bjs.InsertFastOutStockDetail(FInterID,FBillNo, WorkBillNo, PkgsBarCode, Program.userid, len);
            if (result == true)
            {
                ScanQty = bjs.GetFastOutStockScanQty(FBillNo);              
                this.tb_PkgsBarCode.Text = "";
                this.tb_ScanQty.Text = ScanQty.ToString();
                this.tb_PkgsBarCode.Focus();
                DataGridStyle();
                BindData(); 
            }
            else
            {
                MessageBox.Show("插入失败", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
               
                this.tb_PkgsBarCode.Text = "";
                this.tb_PkgsBarCode.Focus();
                return;
            }
        }

        //返回
        private void button2_Click(object sender, EventArgs e)
        {
            FastOutStock iw = new FastOutStock(this.ID);
            iw.Show();
            this.Hide();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FInterid = ((DataTable)(dataGrid1.DataSource)).Rows[index]["FInterid"].ToString();
            PkgsBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PkgsBarCode"].ToString();
        }

    }
}